package com.example.program;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.program.Dao.PassDao;
import com.example.program.bean.LogBean;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mCon;
    private String Account;
    private String Password;
    private EditText log_ed_ac;
    private EditText log_ed_pw;
    private Button log_bt_lg;
    private Button log_bt_ot;
    private CoordinatorLayout log_coor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitVar();
        InitEvent();
    }

    private void InitEvent() {
        log_bt_lg.setOnClickListener(this);
        log_bt_ot.setOnClickListener(this);
    }

    private void InitVar() {
        mCon = MainActivity.this;
        PassDao passDao = new PassDao(mCon);
        LogBean loginAc = passDao.getLoginAc();
        Account = loginAc.LOG_acc;
        Password = loginAc.LOG_pas;
        log_coor = (CoordinatorLayout) findViewById(R.id.log_coor);
        log_ed_ac = (EditText) findViewById(R.id.log_ed_account);
        log_ed_pw = (EditText) findViewById(R.id.log_ed_password);
        log_bt_lg = (Button) findViewById(R.id.log_bt_lg);
        log_bt_ot = (Button) findViewById(R.id.log_bt_ot);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.log_bt_lg:
                if (log_ed_ac.getText().toString().trim().isEmpty() ||
                        log_ed_pw.getText().toString().trim().isEmpty()) {
                    Snackbar.make(log_coor, "请填写账号或密码哦", Snackbar.LENGTH_SHORT).show();
                } else {
                    LOGIN();
                }
                break;
            case R.id.log_bt_ot:
                System.exit(0);
                break;
        }
    }

    private void LOGIN() {
        if (log_ed_ac.getText().toString().trim().equals(Account) &&
                log_ed_pw.getText().toString().trim().equals(Password)) {
            finish();
            Intent in = new Intent(this, PassActivity.class);
            startActivity(in);
        } else {
            Snackbar.make(log_coor, "账号或密码错了哦Σ(っ °Д °;)っ", Snackbar.LENGTH_SHORT).show();
        }
    }
}

